package cn.youqu.happy.module.youqu.service.userJoinActivity;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.module.youqu.controller.admin.userJoinActivity.vo.UserJoinActivityCreateReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userJoinActivity.vo.UserJoinActivityExportReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userJoinActivity.vo.UserJoinActivityPageReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userJoinActivity.vo.UserJoinActivityUpdateReqVO;
import cn.youqu.happy.module.youqu.convert.userJoinActivity.UserJoinActivityConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userJoinActivity.UserJoinActivityDO;
import cn.youqu.happy.module.youqu.dal.mysql.userJoinActivity.UserJoinActivityMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.youqu.happy.module.youqu.enums.ErrorCodeConstants.USER_JOIN_ACTIVITY_NOT_EXISTS;

/**
 * 用户加入活动 Service 实现类
 *
 * @author 王其岩 20112001045
 */
@Service
@Validated
public class UserJoinActivityServiceImpl implements UserJoinActivityService {

    @Resource
    private UserJoinActivityMapper userJoinActivityMapper;

    @Override
    public Integer createUserJoinActivity(UserJoinActivityCreateReqVO createReqVO) {
        // 插入
        UserJoinActivityDO userJoinActivity = UserJoinActivityConvert.INSTANCE.convert(createReqVO);
        userJoinActivityMapper.insert(userJoinActivity);
        // 返回
        return userJoinActivity.getId();
    }

    @Override
    public void updateUserJoinActivity(UserJoinActivityUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserJoinActivityExists(updateReqVO.getId());
        // 更新
        UserJoinActivityDO updateObj = UserJoinActivityConvert.INSTANCE.convert(updateReqVO);
        userJoinActivityMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserJoinActivity(Integer id) {
        // 校验存在
        this.validateUserJoinActivityExists(id);
        // 删除
        userJoinActivityMapper.deleteById(id);
    }

    private void validateUserJoinActivityExists(Integer id) {
        if (userJoinActivityMapper.selectById(id) == null) {
            throw exception(USER_JOIN_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public UserJoinActivityDO getUserJoinActivity(Integer id) {
        return userJoinActivityMapper.selectById(id);
    }

    @Override
    public UserJoinActivityDO getUserJoinActivity(Integer activityId, Long hostUid, Long uid) {
        userJoinActivityMapper.findOneByActivityIdAndHostUidAndUid(activityId, hostUid, uid);
        return null;
    }

    @Override
    public List<UserJoinActivityDO> getUserJoinActivityList(Collection<Integer> ids) {
        return userJoinActivityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserJoinActivityDO> getUserJoinActivityPage(UserJoinActivityPageReqVO pageReqVO) {
        return userJoinActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserJoinActivityDO> getUserJoinActivityList(UserJoinActivityExportReqVO exportReqVO) {
        return userJoinActivityMapper.selectList(exportReqVO);
    }

    @Override
    public Integer joinUserActivity(Integer activityId, Long hostUid, Long uid) {
        UserJoinActivityDO activityDO = userJoinActivityMapper.findOneByActivityIdAndHostUidAndUid(activityId, hostUid, uid);
        if (activityDO == null) {
            UserJoinActivityDO userJoinActivityDO = new UserJoinActivityDO();
            userJoinActivityDO.setActivityId(activityId);
            userJoinActivityDO.setHostUid(hostUid);
            userJoinActivityDO.setUid(uid);

            userJoinActivityMapper.insert(userJoinActivityDO);
            return userJoinActivityDO.getId();
        } else {
            return activityDO.getId();
        }


    }

    @Override
    public boolean quitUserActivity(Integer activityId, Long hostUid, Long uid) {
        //直接删除
        int i = userJoinActivityMapper.deleteByActivityIdAndHostUidAndUid(activityId, hostUid, uid);
        return i > 0;
    }

    @Override
    public List<Long> getUserActivityPartners(Integer activityId, Long hostUid) {
        return userJoinActivityMapper.selectUidByActivityIdAndHostUid(activityId, hostUid);
    }

}
